//
//  NSDate+Extension.swift
//  HYYWeibo
//
//  Created by HY-Mac on 16/7/16.
//  Copyright © 2016年 HY-Mac. All rights reserved.
//

import Foundation

extension NSDate{
    class func createWeiboDateFromString(timeStr: String, formatStr: String) -> NSDate{
        let formatter = NSDateFormatter()
        formatter.dateFormat = formatStr
        formatter.locale = NSLocale(localeIdentifier: "en")
        return formatter.dateFromString(timeStr)!
    }
    func createWeiboTimeString(createDate: NSDate) -> String{
        
        let calendar = NSCalendar.currentCalendar()
        //获取两个时间的差值
        var formatterStr = "HH:mm"
        let formatter = NSDateFormatter()
        if calendar.isDateInToday(createDate){
            // today
            //compare these two dates
            let interval = Int(NSDate().timeIntervalSinceDate(createDate))
            if interval < 60{
                return "刚刚"
            }else if interval < 60 * 60{
                return "\(interval / 60)分钟前"
            }else if interval < 60 * 60 * 24{
                return "\(interval / (60 * 60))小时前"
            }
        }else if calendar.isDateInYesterday(createDate){
            formatterStr = "昨天" + formatterStr
        }else{
            let comps = calendar.components(.Year, fromDate: createDate, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))
            HYLog(comps.year)
            if comps.year >= 1{
                // more than 1 year
                formatterStr = "yyyy-MM-dd " + formatterStr
            }else{
                // less than 1 year
                formatterStr = "MM-dd " + formatterStr
            }
        }
        formatter.dateFormat = formatterStr
        return formatter.stringFromDate(createDate)
    }
}
